package com.yonyou.pmclouds.billview.rmiimpl.buzi;

import com.yonyou.pmclouds.alterandvisa.entity.AlterAndVisaVO;
import com.yonyou.pmclouds.alterandvisa.rmiitf.AlterAndVisaQuery;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.billview.util.BillViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;

@Component
public class VisaBuziServiceImpl implements IBillViewBuziService {

    @Autowired
    private AlterAndVisaQuery alterAndVisaQuery;

    @Override
    public String getBillType() {
        return BillTypeConst.ALTER_AND_VISA_BILLTYPE;
    }

    @Override
    public PageObject<Object> queryBillList(BillViewQueryVO billViewQueryVO) throws BusinessException {
        PageObject<Object> pageResult = alterAndVisaQuery.queryByCondition(billViewQueryVO.getPkProject(),billViewQueryVO.getCreators(),billViewQueryVO.getStartTime(),
                billViewQueryVO.getEndTime(),billViewQueryVO.getBillStatus(),billViewQueryVO.getPageStart(),billViewQueryVO.getPageSize());

        Object[] objects = pageResult.getObjects();

        DecimalFormat df = new DecimalFormat( "0.00");
        AlterAndVisaVO tempVO = new AlterAndVisaVO();
        for(Object obj : objects){
            tempVO = ((AlterAndVisaVO)obj);
            if(tempVO.getEstimateMny() == null){
                tempVO.setStrEstimateMny("");
            }else{
                tempVO.setStrEstimateMny(df.format(tempVO.getEstimateMny().doubleValue()));
            }
        }
        pageResult.setObjects(objects);

        BillViewUtil.setBillStatus(pageResult.getObjects());

        return  pageResult;
    }

    @Override
    public SuperVO queryBillDetail(String pkBill) throws BusinessException {

        AlterAndVisaVO alterAndVisaVO = alterAndVisaQuery.getById(pkBill);

        if(alterAndVisaVO != null && alterAndVisaVO.getEstimateMny() != null){
            DecimalFormat df = new DecimalFormat( "0.00");
            if(alterAndVisaVO.getEstimateMny() == null){
                alterAndVisaVO.setStrEstimateMny("");
            }else{
                alterAndVisaVO.setStrEstimateMny(df.format(alterAndVisaVO.getEstimateMny().doubleValue()));
            }
        }

        BillViewUtil.setBillStatus(new AlterAndVisaVO[]{alterAndVisaVO});
        BillViewUtil.setFormatDate(new AlterAndVisaVO[]{alterAndVisaVO},new String[]{CommonFieldConst.CREATIONTIME,"applyDate"},new String[]{"strCreationtime","strApplyDate"});

        return alterAndVisaVO;
    }
}
